package get

import (
	"database/sql"
	_"strconv"

	"github.com/gin-gonic/gin"
)

func Get_view(r *gin.Engine) {
		var db *sql.DB
		var err error
	
		db, err = sql.Open("postgres", "port=5432 user=postgres password=system dbname=databasedesign sslmode=disable")
		if err != nil {
			panic(err)
		}
	
		r.GET("/getview", func(c *gin.Context) {
			var ISTJ, ISFJ, INFJ, INTJ, ISTP, ISFP, INFP, INTP, ESTP, ESFP, ENFP, ENTP, ESTJ, ESFJ, ENFJ, ENTJ string = "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"
			var mal_count [16]int
	
			str1 := "SELECT count(*) FROM test group by mal having mal='" + ISTJ + "'"
			rows1, err := db.Query(str1)
			if err != nil {
				panic(err)
			}
			for rows1.Next() {
				err = rows1.Scan(&mal_count[0])
				if err != nil {
					panic(err)
				}
			}
	
			str2 := "SELECT count(*) FROM test group by mal having mal='" + ISFJ + "'"
			rows2, err := db.Query(str2)
			if err != nil {
				panic(err)
			}
			for rows2.Next() {
				err = rows2.Scan(&mal_count[1])
				if err != nil {
					panic(err)
				}
			}
	
			str3 := "SELECT count(*) FROM test group by mal having mal='" + INFJ + "'"
			rows3, err := db.Query(str3)
			if err != nil {
				panic(err)
			}
			for rows3.Next() {
				err = rows3.Scan(&mal_count[2])
				if err != nil {
					panic(err)
				}
			}
	
			str4 := "SELECT count(*) FROM test group by mal having mal='" + INTJ + "'"
			rows4, err := db.Query(str4)
			if err != nil {
				panic(err)
			}
			for rows4.Next() {
				err = rows4.Scan(&mal_count[3])
				if err != nil {
					panic(err)
				}
			}
	
			str5 := "SELECT count(*) FROM test group by mal having mal='" + ISTP + "'"
			rows5, err := db.Query(str5)
			if err != nil {
				panic(err)
			}
			for rows5.Next() {
				err = rows5.Scan(&mal_count[4])
				if err != nil {
					panic(err)
				}
			}
	
			str6 := "SELECT count(*) FROM test group by mal having mal='" + ISFP + "'"
			rows6, err := db.Query(str6)
			if err != nil {
				panic(err)
			}
			for rows6.Next() {
				err = rows6.Scan(&mal_count[5])
				if err != nil {
					panic(err)
				}
			}
	
			str7 := "SELECT count(*) FROM test group by mal having mal='" + INFP + "'"
			rows7, err := db.Query(str7)
			if err != nil {
				panic(err)
			}
			for rows7.Next() {
				err = rows7.Scan(&mal_count[6])
				if err != nil {
					panic(err)
				}
			}
	
			str8 := "SELECT count(*) FROM test group by mal having mal='" + INTP + "'"
			rows8, err := db.Query(str8)
			if err != nil {
				panic(err)
			}
			for rows8.Next() {
				err = rows8.Scan(&mal_count[7])
				if err != nil {
					panic(err)
				}
			}
	
			str9 := "SELECT count(*) FROM test group by mal having mal='" + ESTP + "'"
			rows9, err := db.Query(str9)
			if err != nil {
				panic(err)
			}
			for rows9.Next() {
				err = rows9.Scan(&mal_count[8])
				if err != nil {
					panic(err)
				}
			}
	
			str10 := "SELECT count(*) FROM test group by mal having mal='" + ESFP + "'"
			rows10, err := db.Query(str10)
			if err != nil {
				panic(err)
			}
			for rows10.Next() {
				err = rows10.Scan(&mal_count[9])
				if err != nil {
					panic(err)
				}
			}
	
			str11 := "SELECT count(*) FROM test group by mal having mal='" + ENFP + "'"
			rows11, err := db.Query(str11)
			if err != nil {
				panic(err)
			}
			for rows11.Next() {
				err = rows11.Scan(&mal_count[10])
				if err != nil {
					panic(err)
				}
			}
	
			str12 := "SELECT count(*) FROM test group by mal having mal='" + ENTP + "'"
			rows12, err := db.Query(str12)
			if err != nil {
				panic(err)
			}
			for rows12.Next() {
				err = rows12.Scan(&mal_count[11])
				if err != nil {
					panic(err)
				}
			}
	
			str13 := "SELECT count(*) FROM test group by mal having mal='" + ESTJ + "'"
			rows13, err := db.Query(str13)
			if err != nil {
				panic(err)
			}
			for rows13.Next() {
				err = rows13.Scan(&mal_count[12])
				if err != nil {
					panic(err)
				}
			}
	
			str14 := "SELECT count(*) FROM test group by mal having mal='" + ESFJ + "'"
			rows14, err := db.Query(str14)
			if err != nil {
				panic(err)
			}
			for rows14.Next() {
				err = rows14.Scan(&mal_count[13])
				if err != nil {
					panic(err)
				}
			}
	
			str15 := "SELECT count(*) FROM test group by mal having mal='" + ENFJ + "'"
			rows15, err := db.Query(str15)
			if err != nil {
				panic(err)
			}
			for rows15.Next() {
				err = rows15.Scan(&mal_count[14])
				if err != nil {
					panic(err)
				}
			}
	
			str16 := "SELECT count(*) FROM test group by mal having mal='" + ENTJ + "'"
			rows16, err := db.Query(str16)
			if err != nil {
				panic(err)
			}
			for rows16.Next() {
				err = rows16.Scan(&mal_count[15])
				if err != nil {
					panic(err)
				}
			}

            type mbti struct{
			Name	string `json:"name"`
			Value 	int `json:"value"`
		}

		var arr [16]mbti = [16]mbti{
			mbti{"ISTJ",mal_count[0]},
			mbti{"ISFJ",mal_count[1]},
			mbti{"INFJ",mal_count[2]},
			mbti{"INTJ",mal_count[3]},
			mbti{"ISTP",mal_count[4]},
			mbti{"ISFP",mal_count[5]},
			mbti{"INFP",mal_count[6]},
			mbti{"INTP",mal_count[7]},
			mbti{"ESTP",mal_count[8]},
			mbti{"ESFP",mal_count[9]},
			mbti{"ENFP",mal_count[10]},
			mbti{"ENTP",mal_count[11]},
			mbti{"ESTJ",mal_count[12]},
			mbti{"ESFJ",mal_count[13]},
			mbti{"ENFJ",mal_count[14]},
			mbti{"ENTJ",mal_count[15]},
		}



		c.JSON(200, gin.H{
			"data": arr,
		})
	
		})
	
		// 获取搜索
		// r.GET("/getview", func(c *gin.Context) {
		// 	xx := c.Query("xx")
		// 	var per, per2 string = "0", "0"
		// 	str := "SELECT count(*) FROM test group by mal having mal='" + xx + "'"
		// 	rows, err := db.Query(str)
		// 	if err != nil {
		// 		panic(err)
		// 	}
	
		// 	for rows.Next() {
	
		// 		err = rows.Scan(&per)
		// 		if err != nil {
		// 			panic(err)
		// 		}
	
		// 	}
	
		// 	//基数
		// 	str2 := "SELECT count(*)FROM test"
		// 	rows2, err := db.Query(str2)
		// 	if err != nil {
		// 		panic(err)
		// 	}
	
		// 	for rows2.Next() {
		// 		err = rows2.Scan(&per2)
		// 		if err != nil {
		// 			panic(err)
		// 		}
		// 	}
		// 	var int3 float64
		// 	int1, err := strconv.ParseFloat(per, 64)
		// 	int2, err := strconv.ParseFloat(per2, 64)
		// 	int3 = (int1 / int2) * 100
		// 	c.JSON(200, gin.H{
		// 		"per":  int3,
		// 		"mal":  xx,
		// 		"int1": int1,
		// 		"int2": int2,
		// 	})
		// })
	}